package com.jiaquyun.jcyx.main.mine

import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ActivityUtils
import com.jiaquyun.jcyx.R
import com.jiaquyun.jcyx.app.JCApplication
import com.jiaquyun.jcyx.jetpack.mode.MineViewModel
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_mine_yong_apply.*

class MineYongApplyActivity : OwnerViewModeActivity<MineViewModel>() {

    companion object {
        fun open() {
            ActivityUtils.startActivity(MineYongApplyActivity::class.java)
        }
    }

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_yong_apply

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = true, color = R.color.white, view = flMineYongApplyBar)
    }

    override fun initView() {

        ivAppBack.setOnClickListener { finish() }

        JCApplication.userIndex?.userInfo?.commission_money?.let {
            tvMineYongApplyMoney.text = "￥${it}"
        }
        tvMineYongApplySubmit.setOnClickListener {
            val money = etMineYongApplyContent.text?.toString()
            if (money.isNullOrEmpty()) {
                toast("请输入金额")
            } else {
                viewModel.userYongApply(money)
            }
        }

    }

    override fun initData() {
        viewModel.userIndex()
    }

    override fun initViewMode() {

        viewModel.userIndexLiveDate.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            tvMineYongApplyMoney.text = "￥${data.userInfo.commission_money}"
        })

        viewModel.userYongApplyLiveDate.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            finish()
        })
    }

}